﻿
/*
 * PsTFS is a set of PowerShell commands to manage and use Team Foundation Server. PowerShell 
 * effectiveness is combined with the power of TFS API to maximizes TFS capabilities.
 * 
 * Creator : Choulant Noham
 * Mail : choulant@gmail.com
 * Blog : http://choulant.blogspot.com
 * 
 */

using System;
using System.Management.Automation;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using PsTFS.Provider;
using PsTFS.Objects;
using PsTFS.Objects.Methodes;


namespace PsTFS.Commandes.Add
{
    /// <summary>
    /// Ajout d'un workitem
    /// </summary>
    [Cmdlet(VerbsCommon.Add, "WorkItem")]
    public class Add_WI : PsCmdlet
    {
        /// <summary>
        /// Constructeur et initialisation des données
        /// </summary>
        public Add_WI()
        {
            this.Type = string.Empty;
            this.Title = string.Empty;
            this.Param = null;
        }
        /// <summary>
        /// Lancement du processus
        /// </summary>
        protected override void ProcessRecord()
        {
            try
            {
                WorkItem createWi = Methode.CreateWI(Context.GetContext().Tfs, Context.GetContext().Project.Name, this.Type, this.Title, this.Param);
                //Information
                if (createWi != null)
                {
                    Log.AddLog(null, "Successfully created workitem " + this.Title);
                    Console.WriteLine("Successfully created workitem " + this.Title);
                }
                else
                {
                    Log.AddLog("Failed workitem " + this.Title);
                    Console.WriteLine("Failed workitem " + this.Title);
                }

                //Renvois les informations
                WriteObject(createWi);
            }
            catch (Exception ex)
            {
                ErrorRecord err = new ErrorRecord(ex, Error.GetInstance().GetError(this), ErrorCategory.WriteError, null);
                WriteError(err);
                Log.AddLog(ex.StackTrace);
            }
            finally
            {
                base.ProcessRecord();
            }
        }

        /// <summary>
        /// Permet d'informer la fin de la commande
        /// </summary>
        protected override void EndProcessing()
        {
            Log.AddLog(null, "Add-Wi is end");
            base.EndProcessing();
        }

        /// <summary>
        /// Object contenant Team Foundation Server
        /// </summary>
        [Parameter(Position = 0)]
        public TeamFoundationServer TFS
        {
            set { Context.GetContext().Tfs = value; }
        }

        /// <summary>
        /// Nom du label
        /// </summary>
        [Parameter(Position = 1)]
        public string ProjectName
        {
            set { Context.GetContext().Project = Methode.GetProject(value); }
        }

        /// <summary>
        /// Type du workitem a créer
        /// </summary>
        [Parameter(Position = 2, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public string Type { get; set; }

        /// <summary>
        /// Titre du workitem
        /// </summary>
        [Parameter(Position = 3, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public string Title { get; set; }

        /// <summary>
        /// Elements à ajouter au workitem
        /// </summary>
        [Parameter(Position = 4)]
        [ValidateNotNullOrEmpty]
        public string[] Param { get; set; }
    }
}
